package com.mcrmb;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class Json {

    private String HttpGet(String urlname){//http get函数
        StringBuffer readOneLineBuff = new StringBuffer();
        String content ="";
        try {
            URL url = new URL(urlname);
            URLConnection conn = url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = "";
            while ((line = reader.readLine()) != null) {
                readOneLineBuff.append(line);
            }
            content = readOneLineBuff.toString();
            reader.close();
        }catch (Exception e3){
            return null;
        }
        return content;
    }

    public ArrayList Json2Array(String url){//Json内容转换为定义好的ArrayList数组.
        String json = this.HttpGet(url);
        JSONObject jsonObject = new JSONObject(json);
        //System.out.println("JSON内容:"+json);//todo后面必须去掉
        ArrayList arr = new ArrayList();
        arr.add(jsonObject.get("code"));
        arr.add(jsonObject.get("msg"));
        try {
            arr.add(jsonObject.getJSONArray("data"));
        }catch (JSONException e){
        }
        return arr;
    }
}
